package com.cash.loan.modular.cash.controller;

import com.cash.loan.common.annotation.UserLogin;
import com.cash.loan.common.entity.UserInfo;
import com.cash.loan.common.result.ServerResponse;
import com.cash.loan.common.util.RedisUtil;
import com.cash.loan.config.properties.CacheTimeProperties;
import com.cash.loan.core.base.controller.BaseController;
import com.cash.loan.modular.cash.AdNameEnum;
import com.cash.loan.modular.cash.service.ChannelVersionService;
import com.cash.loan.modular.cash.service.ProductIndexService;
import com.cash.loan.modular.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Date 2018-07-06 14:07
 */
@Api(description = "首页接口")
@RestController
@RequestMapping(value = "/index")
public class IndexController extends BaseController {

    private Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ProductIndexService indexService;

    @Autowired
    private CacheTimeProperties cacheTimeProperties;

    @Autowired
    private UserService userService;

    @Autowired
    private ChannelVersionService channelVersionService;

    /**
     * 首页头部和新品查询接口
     * @return
     */
    @ApiOperation("首页展示数据")
    @GetMapping()
    public ServerResponse<Map<String,Object>> getIndex(@RequestHeader(value = "uitype",required = false,defaultValue = "0") Integer uiType) {
        System.out.println(uiType);
        //首页轮播展示
        List<Map<String,Object>> adPictureList = indexService.getIndexHeader(AdNameEnum.OLD.getName(),uiType);
        //新品展示
        List<Map<String,Object>> newProductResult = indexService.getNewProduct(uiType);
        //首页热销产品展示
        List<Map<String,Object>> hotProductResult = indexService.getHotProduct(uiType);
        Map<String,Object> result = new HashMap<>(6);
        result.put("adList",adPictureList);
        result.put("newProductList",newProductResult);
        result.put("hotProductList",hotProductResult);
        return ServerResponse.getSuccess("success",result);
    }

    /**
     * 首页内容组装，v1.1.0版本
     * @return
     */
    @ApiOperation("新版首页展示数据")
    @GetMapping(value = "/v2")
    public ServerResponse<Map<String,Object>> getIndexV2(@RequestHeader(value = "uitype",required = false,defaultValue = "0") Integer uiType) {
        //首页轮播展示
        List<Map<String,Object>> adPictureList = indexService.getIndexHeader(AdNameEnum.INDEX_AD.getName(),uiType);
        //首页分类展示
        List<Map<String,Object>> productCateList = indexService.getProductCate();
        //推荐展示
        Map<String,Map<String,Object>> recommendMap = indexService.getRecommendList(uiType);
        //首页热销产品展示
        List<Map<String,Object>> hotProductResult = indexService.getHotProduct(uiType);
        Map<String,Object> result = new HashMap<>(8);
        result.put("adList",adPictureList);
        result.put("cateList",productCateList);
        result.put("recommend",recommendMap);
        result.put("hotProductList",hotProductResult);
        return ServerResponse.getSuccess("success",result);
    }

    /**
     * 记录imei到登录表中
     * @param request
     * @return
     */
    @ApiOperation("首页记录deviceId")
    @PostMapping(value = "/record")
    public ServerResponse<Map<String,Object>> recordImei(@UserLogin UserInfo userInfo,
                                     @RequestHeader(value = "Access-Token",required = false,defaultValue = "no-token") @ApiParam(value = "没有token时可不传") String token,
                                     HttpServletRequest request) {
        //当查询的用户不为空时,说明用户已经登录，这时应该刷新token
        Map<String,Object> map = new HashMap<>(2);
        if(userInfo != null) {
            String newTokenName = userService.productTokenName();
            redisUtil.rename(token,newTokenName);
            redisUtil.updateExpireTime(newTokenName,cacheTimeProperties.getToken());
            map.put("newToken",newTokenName);
            return ServerResponse.getSuccess("success",map);
        } else {
            map.put("newToken","");
            return ServerResponse.getSuccess("success",map);
        }
    }

    @ApiOperation("根据市场渠道号和app版本号得到ui类型")
    @ApiResponse(code = 0, message = "uiType=0表示用户UI，uiType=1表示送审UI")
    @GetMapping(value = "/ui")
    public ServerResponse<Map<String,Integer>> choiceUi(@RequestParam(value = "channelCode") String channelCode,
                                   @RequestParam(value = "version") String version) {
        if(StringUtils.isBlank(channelCode) || StringUtils.isBlank(version)) {
            return ServerResponse.getErrorMessage("参数错误");
        }

        Integer result = channelVersionService.getUiType(channelCode,version);

        Map<String,Integer>  map = new HashMap<>(2);

        if(result != null) {
            map.put("uiType",result);
        } else {
            map.put("uiType",0);
        }

        return ServerResponse.getSuccess("success",map);
    }
}
